/*
 * RepositoryPackagerView.java
 */
package repositorypackager;

import datasets.jaxb.Datasets;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

import javax.swing.JOptionPane;
import monash.med.crystallography.DataSetTools.DiffdumpXML.DiffdumpXML;
import monash.med.crystallography.DataSetTools.TarBzipper.TarBzipper;
import monash.med.crystallography.DataSetTools.FileSplitter.FileSplitter;

/**
 * The application's main frame.
 */
public class RepositoryPackagerView extends FrameView {

     public String locate() throws ClassNotFoundException, UnsupportedEncodingException {

        final URL location;
        final String classLocation = this.getClass().getName().replace('.', '/') + ".class";
        final ClassLoader loader = this.getClass().getClassLoader();
        if( loader == null )
        {
          location = ClassLoader.getSystemResource(classLocation);
        }
        else
        {
          location = loader.getResource(classLocation);
        }
        if( location != null ) 
        {
          Pattern p = Pattern.compile( "^.*:(.*)!.*$" ) ;

          Matcher m = p.matcher( URLDecoder.decode(location.getPath(), "UTF-8") );
          if( m.find() )
            return new File(m.group( 1 )).getParent();
          else
            throw new ClassNotFoundException( "Cannot parse location of '" + location + "'.  Probably not loaded from a Jar" ) ;
        }
        else 
          throw new ClassNotFoundException( "Cannot find class '" + this.getClass().getName() + " using the classloader" ) ;
  }        
    
    public RepositoryPackagerView(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        String resource = "";
        
        cmbXraySource.setSelectedIndex(1);
        
        try{
        resource = locate();
        
        String strImagePath = resource + File.separator + "Example" + File.separator + "2NW0 Diffraction Images";
        
        String strOutputPath = resource + File.separator + "Example" + File.separator + "Upload";       
        
        txtImagePath.setText(strImagePath);
        txtOutputPath.setText(strOutputPath);            
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        
        System.setOut( new PrintStream(
        new ConsoleView (txtConsoleView, System.out), true));

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();

            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = RepositoryPackagerApp.getApplication().getMainFrame();
            aboutBox = new RepositoryPackagerAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        RepositoryPackagerApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtConsoleView = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        cmbXraySource = new javax.swing.JComboBox();
        txtResolutionLimit = new javax.swing.JTextField();
        txtDiffractometerType = new javax.swing.JTextField();
        txtDatasetName = new javax.swing.JTextField();
        txtCrystalName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtImagePath = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtOutputPath = new javax.swing.JTextField();
        btnDiffractionDirBrowse = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtChunkSize = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        txtMosiacSpread = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtChiAngle = new javax.swing.JTextField();
        btnGo = new javax.swing.JButton();
        btnOutputDirBrowse = new javax.swing.JButton();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtConsoleView.setColumns(20);
        txtConsoleView.setEditable(false);
        txtConsoleView.setRows(5);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(repositorypackager.RepositoryPackagerApp.class).getContext().getResourceMap(RepositoryPackagerView.class);
        txtConsoleView.setText(resourceMap.getString("txtConsoleOutput.text")); // NOI18N
        txtConsoleView.setName("txtConsoleOutput"); // NOI18N
        jScrollPane1.setViewportView(txtConsoleView);

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        cmbXraySource.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Synchrotron", "Rotating Anode", "Tube" }));
        cmbXraySource.setName("cmbXraySource"); // NOI18N

        txtResolutionLimit.setText(resourceMap.getString("txtResolutionLimit.text")); // NOI18N
        txtResolutionLimit.setName("txtResolutionLimit"); // NOI18N

        txtDiffractometerType.setText(resourceMap.getString("txtDiffractometerType.text")); // NOI18N
        txtDiffractometerType.setName("txtDiffractometerType"); // NOI18N

        txtDatasetName.setText(resourceMap.getString("txtDatasetName.text")); // NOI18N
        txtDatasetName.setName("txtDatasetName"); // NOI18N

        txtCrystalName.setText(resourceMap.getString("txtCrystalName.text")); // NOI18N
        txtCrystalName.setName("txtCrystalName"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtImagePath.setText(resourceMap.getString("txtImagePath.text")); // NOI18N
        txtImagePath.setName("txtImagePath"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtOutputPath.setName("txtOutputPath"); // NOI18N

        btnDiffractionDirBrowse.setText(resourceMap.getString("btnDiffractionDirBrowse.text")); // NOI18N
        btnDiffractionDirBrowse.setName("btnDiffractionDirBrowse"); // NOI18N
        btnDiffractionDirBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDiffractiondirBrowseAction(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtChunkSize.setText(resourceMap.getString("txtChunkSize.text")); // NOI18N
        txtChunkSize.setName("txtChunkSize"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        txtMosiacSpread.setText(resourceMap.getString("txtMosiacSpread.text")); // NOI18N
        txtMosiacSpread.setName("txtMosiacSpread"); // NOI18N

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        txtChiAngle.setText(resourceMap.getString("txtChiAngle.text")); // NOI18N
        txtChiAngle.setName("txtChiAngle"); // NOI18N

        btnGo.setText(resourceMap.getString("btnGo.text")); // NOI18N
        btnGo.setName("btnGo"); // NOI18N
        btnGo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGoAction(evt);
            }
        });

        btnOutputDirBrowse.setText(resourceMap.getString("btnOutputDirBrowse.text")); // NOI18N
        btnOutputDirBrowse.setName("btnOutputDirBrowse"); // NOI18N
        btnOutputDirBrowse.setRequestFocusEnabled(false);
        btnOutputDirBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOutputDirBrowseAction(evt);
            }
        });

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .add(mainPanelLayout.createSequentialGroup()
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, txtOutputPath, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE)
                            .add(txtImagePath, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 542, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, mainPanelLayout.createSequentialGroup()
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 279, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel8))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                    .add(txtDatasetName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                                    .add(txtResolutionLimit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(mainPanelLayout.createSequentialGroup()
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(cmbXraySource, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                            .add(txtDiffractometerType)))
                                    .add(txtCrystalName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 244, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 9, Short.MAX_VALUE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(btnOutputDirBrowse, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(btnDiffractionDirBrowse, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel7)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel9)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel10)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 641, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, mainPanelLayout.createSequentialGroup()
                        .add(jLabel11)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(txtMosiacSpread, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel12)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(txtChiAngle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, mainPanelLayout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(txtChunkSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnGo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanelLayout.createSequentialGroup()
                .add(20, 20, 20)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(txtImagePath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnDiffractionDirBrowse))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnOutputDirBrowse)
                    .add(txtOutputPath, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(15, 15, 15)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(txtDatasetName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel6))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(txtCrystalName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(txtResolutionLimit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel9)
                    .add(txtDiffractometerType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel10)
                    .add(cmbXraySource, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel11)
                    .add(txtMosiacSpread, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel12)
                    .add(txtChiAngle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(3, 3, 3)
                .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(txtChunkSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3)
                    .add(btnGo)
                    .add(jLabel4))
                .add(8, 8, 8)
                .add(jLabel5)
                .add(14, 14, 14)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                .add(34, 34, 34))
        );

        setComponent(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void btnDiffractiondirBrowseAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDiffractiondirBrowseAction
        if (evt.getSource() == btnDiffractionDirBrowse) {
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fc.showOpenDialog(mainPanel);
            
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                txtImagePath.setText(file.getAbsolutePath());
                //This is where a real application would open the file.
                //log.append("Opening: " + file.getName() + "." + newline);
            } else {
               // log.append("Open command cancelled by user." + newline);
            }
        }
    }//GEN-LAST:event_btnDiffractiondirBrowseAction

    private void btnOutputDirBrowseAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOutputDirBrowseAction
        if (evt.getSource() == btnOutputDirBrowse) {
            fco.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fco.showOpenDialog(mainPanel);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fco.getSelectedFile();
                txtOutputPath.setText(file.getAbsolutePath());
                //This is where a real application would open the file.
                //log.append("Opening: " + file.getName() + "." + newline);
            } else {
               // log.append("Open command cancelled by user." + newline);
            }
        }  
    }//GEN-LAST:event_btnOutputDirBrowseAction

    private void btnGoAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGoAction
        Thread worker = new Thread( new HardWorker() );
        worker.setDaemon(true);
        worker.start();
    }//GEN-LAST:event_btnGoAction

    private boolean validateGo()
    {
       Pattern p = Pattern.compile("(^[0-9]*[1-9]+[0-9]*\\.[0-9]*$)|(^[0-9]*\\.[0-9]*[1-9]+[0-9]*$)|(^[0-9]*[1-9]+[0-9]*$)");
       // check for positive decimal value
        try {     
            if(txtImagePath.getText().equals(""))
            {
                throw new Exception("Missing image path");
            } 
            else if(txtOutputPath.getText().equals(""))
            {
                throw new Exception("Missing output path");
            }             
            else if(txtChunkSize.getText().equals(""))
            {
                throw new Exception("Missing chunk size");
            }
            else if(!new File(txtImagePath.getText()).exists())
            {
                throw new Exception("Invalid image path");
            }
            else if(!p.matcher(txtChunkSize.getText()).matches())
            {
                throw new Exception("Invalid chunk value");
            }        
            else if(!txtMosiacSpread.getText().equals(""))
            {
                Float.parseFloat(txtMosiacSpread.getText());
            }
            else if(!txtChiAngle.getText().equals(""))
            {
                Float.parseFloat(txtChiAngle.getText());
            }   
            else if(txtResolutionLimit.getText().equals(""))
            {
                throw new Exception("Resolution is required");
            }                  
            
            Float.parseFloat(txtResolutionLimit.getText());
        }
        catch (NumberFormatException e)
        {
            JOptionPane.showMessageDialog(mainPanel, "Error: Some non-numerical values were found in numerical fields",
				                       "Error", JOptionPane.ERROR_MESSAGE);
            return false;            
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(mainPanel, e.getMessage(),
				                       "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }         

        return true;
  
    }
    
    private void go()
    {
        if(validateGo())
        {
            try
            {
                btnGo.setEnabled(false);
                txtConsoleView.setText("");

                Float chunkSize = new Float(txtChunkSize.getText());     

                String destName = new File(txtImagePath.getText()).getName();
                String destDir = new File(txtOutputPath.getText()).getAbsolutePath();
                ArrayList<Datasets.Dataset.Files.File> filesAdd = new ArrayList();
                
                String destbzipname = "";
                
                if((!new File(destDir + File.separator + destName + ".tar").exists()) &&
                        (!new File(destDir + File.separator + destName + ".tar.bz").exists()) &&
                        (!new File(destDir + File.separator + destName + ".tar.bz.0").exists()))
                {
                    String bzipName = TarBzipper.tarBzip(txtImagePath.getText(), destDir);
                    System.out.println("");
                    
                    destbzipname = destDir + File.separator + new File(bzipName).getName();
                    
                    filesAdd =
                            FileSplitter.splitFile(destbzipname, txtOutputPath.getText(), chunkSize);
                    
                }
                else
                {
                    destbzipname = destDir + File.separator + destName + "-nofiles";
                    System.out.println("WARNING: Destination files already exist in output directory." +
                            "\nFile operations cancelled.");
                }

                System.out.println("");
                DiffdumpXML ddXML = new DiffdumpXML();
                ddXML.extractMetadata(txtImagePath.getText());
                ddXML.setFilesAdd(filesAdd);
                ddXML.setDsName(txtDatasetName.getText());
                ddXML.setCrystalName(txtCrystalName.getText());
                ddXML.setResolutionLimit(Float.parseFloat(txtResolutionLimit.getText()));
                ddXML.setDiffractometerType(txtDiffractometerType.getText());
                ddXML.setXraySource((String)cmbXraySource.getModel().getSelectedItem());

                if(!txtChiAngle.getText().equals(""))
                {
                    ddXML.setChiAngle(Float.parseFloat(txtChiAngle.getText()));
                }

                if(!txtMosiacSpread.getText().equals(""))
                {
                    ddXML.setMosaicSpread(Float.parseFloat(txtMosiacSpread.getText()));
                }            

                ddXML.writeXMLDataset(destbzipname + ".xml");

                File destBzFile = new File(destbzipname);
                                        
                if(destBzFile.exists())
                    destBzFile.delete();
                
                System.out.println("");
                System.out.println("Process Completed");
                System.out.println("Additional files for deposition should be moved to");
                System.out.println(txtOutputPath.getText());
                System.out.println("Before upload.");

                btnGo.setEnabled(true);
            }
            catch(Exception e)
            {
                e.printStackTrace(); // debugging purposes
            }
        }
    }
    
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDiffractionDirBrowse;
    private javax.swing.JButton btnGo;
    private javax.swing.JButton btnOutputDirBrowse;
    private javax.swing.JComboBox cmbXraySource;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTextField txtChiAngle;
    private javax.swing.JTextField txtChunkSize;
    private javax.swing.JTextArea txtConsoleView;
    private javax.swing.JTextField txtCrystalName;
    private javax.swing.JTextField txtDatasetName;
    private javax.swing.JTextField txtDiffractometerType;
    private javax.swing.JTextField txtImagePath;
    private javax.swing.JTextField txtMosiacSpread;
    private javax.swing.JTextField txtOutputPath;
    private javax.swing.JTextField txtResolutionLimit;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
    //Create a file chooser
    final JFileChooser fc = new JFileChooser();
    final JFileChooser fco = new JFileChooser();
    
    
      /**
  * Perform the fetch, and update the GUI elements using the event dispatch 
  * thread.
  *
  * Should be run as a daemon thread, such that it never prevents the application
  * from exiting.
  */
  private final class HardWorker implements Runnable {
    public void run() {
      //simulateLongDelay();
      try {
        // do stuff here
          go();
        //EventQueue.invokeLater( new GuiUpdater(quotes) );
      }
      catch(Exception ex) {
        //EventQueue.invokeLater( new GuiUpdater(ex) );
      }
    }
  }
}
